﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WSNs
{
    public enum DataStutas
    {
        Ok = 0,
        StartErr = 1,
        EndErr = 2,
        SerialTypeErr = 3,
        DataPackRev = 4,
        CmdPackRev = 5,
        AnswerPackRev = 6,
        AddrPackRev = 7,
        PackSerialResErr = 8,
        LinkDectPackRec = 9,
        NodeState = 0x0B,
    }
    /// <summary>
    /// 采样数据格式
    /// </summary>
    public enum DataType
    {
        /// <summary>
        /// 无符号16位数
        /// </summary>
        Unsigned16 = 0, 
        /// <summary>
        /// 有符号16位数
        /// </summary>
        Signed16 = 1, 
        /// <summary>
        /// 有符号24位数
        /// </summary>
        Signed24 = 2, 
        /// <summary>
        /// 实验室格式数据
        /// </summary>
        DaqData = 3,
        /// <summary>
        /// 四通道数据
        /// </summary>
        FourChannels = 4,
    }

    /// <summary>
    /// 串口发送的包类型
    /// </summary>
    public enum SerialPackType : byte
    {
        /// <summary>
        /// 数据包
        /// </summary>
        DataPack = 1,
        /// <summary>
        /// 命令包
        /// </summary>
        CmdPack = 2,
        /// <summary>
        /// 回复包
        /// </summary>
        AnswerPack = 3,
        /// <summary>
        /// 地址关系包
        /// </summary>
        AddrPack = 4,
        /// <summary>
        /// 拓扑关系包
        /// </summary>
        TopoPack = 5,
        /// <summary>
        /// 组建网络命令包
        /// </summary>
        NetworkBuildPack = 6,
        /// <summary>
        /// 路由路退避时间
        /// </summary>
        RouterDelayTime = 7,
        ///<summary>
        ///链路质量检测包
        ///<summary>
        LinkDectPack=9,
        ///<summary>
        ///重组网络包
        ///<summary>
        ReBuild=0x0A,
        /// <summary>
        /// 节点状态
        /// </summary>
        NodeState = 0x0B,
    }

    /// <summary>
    /// 命令类型
    /// </summary>
    public enum CmdType : byte
    {
        /// <summary>
        /// 采集命令
        /// </summary>
        AcqCmd = 1,
        /// <summary>
        /// 传输命令
        /// </summary>
        TransferCmd = 2
    }

    /// <summary>
    /// 采集模式
    /// </summary>
    public enum AcqMode : byte
    {
        /// <summary>
        /// 自由采样
        /// </summary>
        Freedom = 1,
        /// <summary>
        /// 定长采样
        /// </summary>
        FixedLength = 2,
        /// <summary>
        /// 触发并自由
        /// </summary>
        TriggerAndFreedom = 3,
        /// <summary>
        /// 触发并定长
        /// </summary>
        TriggerAndFixedLength = 4,
        /// <summary>
        /// 启动采集
        /// </summary>
        Start = 5,
        /// <summary>
        /// 停止采集
        /// </summary>
        Stop = 6
    }

    /// <summary>
    /// 耦合方式
    /// </summary>
    public enum CoupleMode : byte
    {
        IEPE = 1,
        AC = 2,
        DC = 3
    }

    /// <summary>
    /// 存储位置
    /// </summary>
    public enum StorageLocation : byte
    {
        MicroSD = 1,
        Flash = 2,
        Wireless = 4
    }

    /// <summary>
    /// 传输模式
    /// </summary>
    public enum TransferMode : byte
    {
        /// <summary>
        /// 启动传输
        /// </summary>
        Start = 1,
        /// <summary>
        /// 停止传输
        /// </summary>
        Stop = 2,
        /// <summary>
        /// 上传节点文件名
        /// </summary>
        FileName = 3,
        /// <summary>
        /// 上传状态参数
        /// </summary>
        StatusPtr = 4
    }

    /// <summary>
    /// 采集命令结构体
    /// </summary>
    public struct AcqStructure
    {
        public AcqMode AcqMode;
        public byte NodeID;
        public byte FileID;

        public CoupleMode CoupleMode;
        public float Frequency;
        public UInt32 AcqLength;

        public byte MeasuerRange;
        public byte TrigerV;
        public StorageLocation StorageLocation;
        public byte Null;
    }

    /// <summary>
    /// 自组网命令结构体
    /// </summary>
    public struct NetworkConfigStructure
    {
        /// <summary>
        /// 串口号
        /// </summary>
        public string SPort;

        /// <summary>
        /// 串口速度
        /// </summary>
        public int SerialRate;

        /// <summary>
        /// 组网跳数
        /// </summary>
        public byte Hop;

        /// <summary>
        /// 信标周期
        /// </summary>
        public byte Bo;

        /// <summary>
        /// 超帧周期
        /// </summary>
        public byte So;

        /// <summary>
        /// 最大组网深度
        /// </summary>
        public byte Lm;

        /// <summary>
        /// 最大路由数
        /// </summary>
        public int MaxRouter;

        /// <summary>
        /// 启动延时时间
        /// </summary>
        public byte Tm;
    }

    /// <summary>
    /// 自组网命令结构体
    /// </summary>
  //  public struct RouterStartTime
   // {
        /// <summary>
        /// 路由器地址
        /// </summary>
  //      public int RouterAddress;

        /// <summary>
        /// 下一跳地址
        /// </summary>
    //    public int NextHopAddress;

        /// <summary>
        /// 基本退避时间间隔
        /// </summary>
   //     public byte BaseBackoffTimeInterval;

        /// <summary>
        /// 退避时间
        /// </summary>
 //       public byte BackoffTime;

  //  }

    /// <summary>
    /// 传输命令结构体
    /// </summary>
    public struct DataBackStructure
    {
        /// <summary>
        /// 传输模式
        /// </summary>
        public TransferMode TransferMode;
        /// <summary>
        /// 节点ID
        /// </summary>
        public byte NodeID;
        /// <summary>
        /// 文件ID
        /// </summary>
        public byte FileID;
        /// <summary>
        /// 存储位置
        /// </summary>
        public StorageLocation StorageLocation;
        /// <summary>
        /// 起始位置
        /// </summary>
        public UInt32 StartIndex;
        /// <summary>
        /// 需要的数据长度
        /// </summary>
        public UInt32 Count;
    }

    /// <summary>
    /// 结构体与数据相互转换
    /// </summary>
    class StructBytes
    {
        //将Byte转换为结构体类型
        public byte[] StructToBytes(object structObj, int size)
        {
            int num = 2;
            byte[] bytes = new byte[size];
            IntPtr structPtr = Marshal.AllocHGlobal(size);
            //将结构体拷到分配好的内存空间
            Marshal.StructureToPtr(structObj, structPtr, false);
            //从内存空间拷贝到byte 数组
            Marshal.Copy(structPtr, bytes, 0, size);
            //释放内存空间
            Marshal.FreeHGlobal(structPtr);
            return bytes;

        }

        //将Byte转换为结构体类型
        public object ByteToStruct(byte[] bytes, Type type)
        {
            int size = Marshal.SizeOf(type);
            if (size > bytes.Length)
            {
                return null;
            }
            //分配结构体内存空间
            IntPtr structPtr = Marshal.AllocHGlobal(size);
            //将byte数组拷贝到分配好的内存空间
            Marshal.Copy(bytes, 0, structPtr, size);
            //将内存空间转换为目标结构体
            object obj = Marshal.PtrToStructure(structPtr, type);
            //释放内存空间
            Marshal.FreeHGlobal(structPtr);
            return obj;
        }
    }

}
